package com.aurora.source.source.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.entity.UserEntity;
import com.aurora.source.source.presenter.SmsVerificationPresenter;
import com.aurora.source.source.view.ISmsVerificationView;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.widget.CountDownButtonHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmsVerificationActivity extends BaseWhiteToolbarActivity<SmsVerificationPresenter> implements ISmsVerificationView {


    @BindView(R.id.tv_Note)
    Button tvNote;
    @BindView(R.id.tv_affirm)
    TextView tvAffirm;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    private UserEntity userEntity;

    @Override
    public SmsVerificationPresenter attach() {
        return new SmsVerificationPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_sms_verification;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        userEntity = GreenDaoHelper.getDaoSession().getUserEntityDao().loadAll().get(0);
        etPhone.setText(userEntity.getUserMobile());
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Map<String, String> params = new HashMap<>();
                    params.put("phone", etPhone.getText().toString());
                    params.put("type", "04");
                    basePresenter.smsVerification(activity, params);
                    CountDownButtonHelper helper = new CountDownButtonHelper(tvNote, "倒计时", 120, 1);

                    helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
                        @Override
                        public void finish() {
                            tvNote.setText("再次获取");
                        }
                    });
                    helper.start();

            }
        }); // 获取验证码--end
        simpleToolbar.setMainTitle("修改密码-短信验证");
        simpleToolbar.setLeftVisibility(View.VISIBLE);

    }

    @Override
    public void doBusiness() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_affirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_affirm:
                if (!TextUtils.isEmpty(etPhone.getText().toString())&&!TextUtils.isEmpty(etCode.getText().toString())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("phone", etPhone.getText().toString());
                    bundle.putString("messageCode",etCode.getText().toString() );
                    gotoActivity(ChangePasswordActivity.class, true, bundle);
                } else {
                    showToast("请填写验证码");
                }
                break;
        }
    }


    @Override
    public void onSmsVerificationSuccess(String msg) {
        showToast("短信发送成功");
    }

    @Override
    public void onSmsVerificationFail(String msg) {
        showToast(msg);
    }

}
